using UnityEngine;

namespace PKGames
{
	public class RectSafeAreaAdjustment : MonoBehaviour
	{
		[SerializeField]
		private RectTransform canvas;

		[SerializeField]
		private RectTransform panel;

		private Rect lastSafeArea = new Rect(0f, 0f, 0f, 0f);

		private void ApplySafeArea(Rect area)
		{
			Vector2 position = area.position;
			Vector2 anchorMax = area.position + area.size;
			position.x /= Screen.width;
			position.y /= Screen.height;
			anchorMax.x /= Screen.width;
			anchorMax.y /= Screen.height;
			panel.anchorMin = position;
			panel.anchorMax = anchorMax;
			lastSafeArea = area;
		}

		private void Update()
		{
			Rect safeArea = Screen.safeArea;
			if (safeArea != lastSafeArea)
			{
				ApplySafeArea(safeArea);
			}
		}
	}
}
